﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI
{
    public class DarkProgressForm2 : Form, IProgressForm
    {
        private readonly Form specParent;
        private ProgressForm form;

        public DarkProgressForm2(Form parent)
        {
            this.specParent = parent;

            DarkProgressForm.InitForm(this, parent);

            KeyDown += ProgressForm_KeyDown;
            Load += ProgressForm_Load;
            FormClosed += DarkProgressForm2_FormClosed;
        }

        private void DarkProgressForm2_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (form != null)
                form.Close();
        }

        private static void ProgressForm_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void ProgressForm_Load(object sender, EventArgs e)
        {
            try
            {
                BackColor = Color.LightGray;
                Opacity = 0.75;

                Bounds = specParent != null ? specParent.Bounds : Screen.PrimaryScreen.Bounds;

                form = new ProgressForm(null, null, "Подождите!");
                form.Owner = this;

                form.Show();
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);

                if (form != null)
                    form.Close();
            }
        }

        #region IProgressForm Members
        public string CurrentProgressText
        {
            get
            {
                return form.CurrentProgressText;
            }
            set
            {
                form.CurrentProgressText = value;
            }
        }

        public TimeSpan GetElapsed()
        {
            return form.GetElapsed();
        }
        #endregion
    }
}
